import * as constants from './constants';
import axios from 'axios';
import {
	fromJS
} from 'immutable';

export const getHomeInfo = () => {
	return (dispatch) => {
		axios.get("/api/home.json").then((res) => {
			const rst = res.data.data;
			const action = changeHomeData(rst);
			dispatch(action);
		});
	}
};

export const getMoreList = (page) => {
	return (dispatch) => {
		axios.get("/api/homeList.json?page=" + page).then((res) => {
			const rst = res.data.list;
			const action = addMoreListData(rst, page + 1);
			dispatch(action);
		});
	}
};

export const toggleTopShow = (show) => ({
	type: constants.TOGGLE_SCROLL_SHOW,
	show
})

const changeHomeData = (rst) => ({
	type: constants.CHANGE_HOME_INFO,
	topicList: rst.topicList,
	articleList: rst.articleList,
	recommendList: rst.recommendList,
	writerList: rst.writerList
});

const addMoreListData = (rst, nextPage) => ({
	type: constants.MORE_LIST,
	rst: fromJS(rst),
	nextPage
});